package com.wyw.core.config.support;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;

import com.wyw.base.util.BaseUtil;
import com.wyw.core.config.XmlConfigParse;
import com.wyw.core.secu.dto.XmlConfigType;
import com.wyw.core.util.CacheManager;

public class SecurityUrlMappingRoleConfigParse implements XmlConfigParse{

	@Override
	public void parse(XmlConfigType xmlConfigType, Document xmlConfig) {
		// TODO Auto-generated method stub
		//System.out.println("XmlConfigType:"+xmlConfigType.getXmlConfigType());
		Map<String, Collection<ConfigAttribute>> secuResourceMap = new HashMap<String, Collection<ConfigAttribute>>();
		
		Collection<ConfigAttribute> roleConfigList ;
        ConfigAttribute roleConfig ;    
        
        Element elementRoot= xmlConfig.getRootElement();
        List<Element> resourceList=elementRoot.getChildren();
        String pattern;
        String roleAtt;
        List<String> roleListAtt;
        for(Element resource :resourceList){
        	 pattern =resource.getAttributeValue("pattern");
        	 roleAtt=resource.getAttributeValue("role");
        	 roleListAtt=BaseUtil.splitAsList(roleAtt, ",");
        	 roleConfigList = new ArrayList<ConfigAttribute>();
        	 for(String r:roleListAtt){
        		 roleConfig=new SecurityConfig(r);
        		 roleConfigList.add(roleConfig);
        	 }
        	 secuResourceMap.put(pattern, roleConfigList);
        }
        
        net.sf.ehcache.Element config=new  net.sf.ehcache.Element(CacheManager.security_url_mapping_role_list,secuResourceMap);
        CacheManager.put(config);
	}

}
